<?php
namespace app\admin\controller;
use \think\Db;
use app\admin\service\AdminService;
class Manager extends Admin
{       
    //管理员列表
    public function index()
    {
        $param = $this->request->param();
        $adminService =new AdminService();
        $admin = $adminService->index($param);
        $this->assign("admin", $admin);
        //设置后台面包屑导航,以后直接从数据库读取
        $this->setNav('管理员管理');
        return $this->fetch();
    }
    
    public function add()
    {
        return $this->fetch();
    }
    public function addPost()
    {
        $post     = $_POST;
        $group_id             = $post['group_id'];
        $post['type']         = 2;
        echo ($post['group_id']);
        unset($post['repass']);
        unset($post['group_id']);
        $admin             = model('admin');
        $id                = $admin->save($post);
        $LastId             = $admin->getLastInsID();
        $group['uid']      = $LastId;
        $group['group_id'] = $group_id;
        $result_group      = Db::name('auth_group_access')->insert($group);
        if($id&&$result_group){
            return json(['code'=>1,'msg'=>'管理员添加成功！']);
        }else if($result_group){
            return json(['code'=>1,'msg'=>'角色增加失败！需要重新设置该用户权限！']);
        }else{
            return json(['code'=>1,'msg'=>'增加失败！']);
        }
    }    
    public function update()
    {
        $post = $param = $this->request->param();
        $admin = model('admin');
        $result_group = Db::name('auth_group_access')->where('uid',$post['id'])->setField('group_id', $post['group_id']);
        $arr=[
            'name'=>$post['name'],
            'password'=>st_password($post['password'])
        ];

        $result_admin = Db::name('admin')->where('id',$post['id'])->update($arr);
        if($result_admin&&$result_group){  
            return json(['code'=>1,'msg'=>'管理员更新成功！']);
        }else if($result_group){
            return json(['code'=>1,'msg'=>'角色更新失败！']);
        }else{
            return json(['code'=>1,'msg'=>'更新失败！']);
        }
    }

    public function edit($id)
    {
        $admin = model('admin');
        $join = [
            ['__AUTH_GROUP_ACCESS__ auth_group_access','a.id = auth_group_access.uid'],
        ];
        $field = 'a.*,auth_group_access.group_id';
        $admin = $admin->where('id', $id)->join($join)->alias('a')->field($field)->find();
        $this->assign("admin", $admin);
        return $this->fetch();
    }
    public function del($id)
    {
        $admin = model('admin');
        $result = $admin->where('id', $id)->update(['delete_time' => time()]);
        if($result){
            return json(['code'=>1,'msg'=>'删除成功！']);
        }else {
            return json(['code'=>1,'msg'=>'删除失败！']);
        }
        
    }
    public function delAll()
    {
        $admin = model('admin');
        $ids = empty($_POST['ids']) ? '' : $_POST['ids'];
        if(empty($ids)){
            return json(['code'=>1,'msg'=>'参数错误！']);
        }else {
            
            $ids = implode(',', $ids);
            $where = "id in($ids)";
            $group_where = "uid in($ids)";
        }
        $admin_result = $admin->where($where)->update(['delete_time' => time()]);
        $group_result =Db::name('auth_group_access')->where($group_where)->update(['delete_time' => time()]);
        if($admin_result&&$group_result){
            return json(['code'=>1,'msg'=>'删除成功！']);
        }else {
            return json(['code'=>1,'msg'=>'删除失败！']);
        }
    
    }
    //改变管理员状态
    public function changeState($id,$state)
    {
        $admin = model('admin');
        $result = $admin->where('id', $id)->update(['state' => $state]);
        if($result){
            return json(['code'=>1,'msg'=>'状态设置成功！']);
        }else {
            return json(['code'=>1,'msg'=>'状态设置失败！']);
        }
    
    }
}
